# uncompyle6 version 2.11.5
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.9 (default, Sep 17 2016, 20:26:04) 
# [GCC 4.9.2]
# Embedded file name: D:\project\inleader_frist_client\print_client\core\clientCode_thread.py
# Compiled at: 2013-12-29 04:03:31
__author__ = 'prj'
import commands
import threading
import os
import zipfile
import time
from utils.http import UrllibHttpUtils
from setting import setting, Vfs
from download import AxelPython

class ClientCodeThread(threading.Thread):

    def __init__(self, uiMain):
        threading.Thread.__init__(self)
        self.isQuit = threading.Event()
        self.isQuit.clear()

    def stop(self):
        self.isQuit.set()
        self.join()

    def _execute(self):
        if setting.config is not None and setting.config.has_option('api', 'client_code_file'):
            response = UrllibHttpUtils.callRpc('get_client_zip', {'macaddress': setting.getMac()})
            if response is None:
                return
            client_file = response['clientCodeZip']
            if client_file is not None:
                down_zip_content = os.path.join(Vfs.getTempDir(), 'clientTemp.zip')
                self.ap = AxelPython(client_file, down_zip_content, 8, proxies={})
                self.ap.paxel(client_file, down_zip_content, blocks=6, proxies={})
                print 'download init clientCode success.\n load system...'
                try:
                    with zipfile.ZipFile(down_zip_content) as zip:
                        zip.extractall(Vfs.getRootDir())
                        UrllibHttpUtils.callRpc('update_client_zip_status', {'macaddress': setting.getMac()})
                finally:
                    Vfs.deleteTempFile(down_zip_content)

            else:
                print 'download init clientCode error'
                return
        return

    def run(self):
        try:
            self._execute()
        except:
            setting.logger.exception('error')